package com.leador.core;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.leador.utils.LogUtil;
import com.leador.utils.TraceSeleGenre;
import com.leador.utils.Utils;

/**
 * 轨迹服务, 开启轨迹服务前（调用LBSTraceClient类中startTrace()方法），需先初始化该类。
 */
public class Trace {
    //上下文
    private Context context;
    //serviceId
    private static long serviceId;
    //entityName
    private static String entityName = "";
    //key
    private String ak = "";
    //证书和编码
    private String mcode = "";
    private static int gatherInterval = 5;
    private static int packInterval = 30;
    private static int distanceInterval = 0;
    //类型（默认 Http）
    public static int traceType = TraceSeleGenre.DATA_HHTP.getValue();

    /**
     * 构造器
     * @param context 上下文
     */
    public Trace(Context context) {
        this.context = context;
        getKey();
        setMcode();
        LogUtil.setContext(context);
    }

    /**
     * 构造器
     * @param context 上下文
     * @param serviceId 服务ID
     * @param entityName  实例名称
     */
    public Trace(Context context, long serviceId, String entityName) {
        this.context = context;
        this.serviceId = serviceId;
        this.entityName = entityName;
        getKey();
        setMcode();
        LogUtil.setContext(context);
    }

    /**
     * 构造器
     * @param context 上下文
     * @param serviceId 服务ID
     * @param entityName  实例名称
     * @param traceType   轨迹服务类型   2 : 建立socket长连接并上传位置数据） 3: http连接 默认2
     */
    public Trace(Context context, long serviceId, String entityName, int traceType) {
        this.context = context;
        this.serviceId = serviceId;
        this.entityName = entityName;
        getKey();
        setMcode();
        setTraceType(traceType);
        LogUtil.setContext(context);
    }

    /**
     * 设置轨迹服务类型
     * @param traceType
     */
    public static void setTraceType(int traceType) {
        if (Utils.checkTypeLegal(traceType, TraceSeleGenre.NOT_SOCKET_CONNECT.getValue(), TraceSeleGenre.DATA_HHTP.getValue())) {
            Trace.traceType = traceType;
        }
    }


    private void setMcode() {
        try
        {
            //3A:77:C1:59:60:C1:C0:D4:AE:26:5D:88:2F:27:A9:A7:9D:C4:B5:7C;com.baidu.trackshow
            this.mcode = Utils.getPackCertPath(this.context);
            return;
        }
        catch (Exception localException) {
            this.mcode = "";
        }
    }

    //-----------------------------初始化Key-------------------------------------------------
    /**
     * 初始化参数
     */
    private void getKey() {
        String key = getKey(context);
        this.ak = key;
        if ((this.ak != null) && (this.ak.length() > 0))
        {
            int i = this.ak.length();
            if ((!TextUtils.isEmpty(this.ak)) && (i > 0) && (i < 32)) {
                for (int j = 0; j < 32 - i; j++) {
                    this.ak += '\000';
                }
            }
        }
    }

    private static String apiKey = "";
    /**
     * 获取key值
     * @param context
     * @return
     */
    public static String getKey(Context context) {
//        try {
//            if ((apiKey == null) || (apiKey.equals(""))) {
//                ApplicationInfo aapinfo = context
//                        .getPackageManager().getApplicationInfo(
//                                context.getPackageName(), PackageManager.GET_META_DATA);
//                if (aapinfo == null||aapinfo.metaData==null) {
//                    return apiKey;
//                }
//                apiKey = aapinfo.metaData.getString("com.leador.apikey");
//            }
//        } catch (PackageManager.NameNotFoundException e) {
//            apiKey = "";
//        }
//        return apiKey;
//        return "ba2e2d74a4f9022e53ab22b6fed8ee53";
//        return "342d2a98958002e65455dccc757fbea1";
        return "ec85d3648154874552835438ac6a02b2";
    }

    /**
     * 返回 上下文
     * @return
     */
    public Context getContext() {
        return context;
    }

    /**
     * 设置 上下文
     * @param context
     */
    public void setContext(Context context) {
        this.context = context;
    }

    /**
     * 返回 获取轨迹服务标识
     * @return 获取轨迹服务标识
     */
    public  long getServiceId() {
        return serviceId;
    }

    /**
     * 设置 获取轨迹服务标识
     * @param serviceId 获取轨迹服务标识
     */
    public void setServiceId(long serviceId) {
        this.serviceId = serviceId;
    }

    /**
     * 返回 entity标识
     * @return
     */
    public String getEntityName() {
        return entityName;
    }

    /**
     * 设置 entity标识
     * @param entityName
     */
    public void setEntityName(String entityName) {
        this.entityName = entityName;
    }

    /**
     * 返回密钥
     * @return
     */
    public String getAk() {
        return ak;
    }

    /**
     * 设置密钥
     * @param ak
     */
    public void setAk(String ak) {
        this.ak = ak;
    }

    public String getMcode() {
        return mcode;
    }

    public void setMcode(String mcode) {
        this.mcode = mcode;
    }

    /**
     * 获取采集周期
     * @return
     */
    public static int getGatherInterval() {
        return gatherInterval;
    }

    /**
     * 设置采集周期
     * @param gatherInterval
     */
    public static void setGatherInterval(int gatherInterval) {
        Trace.gatherInterval = gatherInterval;
    }

    /**
     * 获取发包周期
     * @return
     */
    public static int getPackInterval() {
        return packInterval;
    }

    /**
     * 设置发包周期
     * @param packInterval
     */
    public static void setPackInterval(int packInterval) {
        Trace.packInterval = packInterval;
    }

    /**
     * 获取距离周期
     * @return
     */
    public static int getDistanceInterval() {
        return distanceInterval;
    }

    /**
     * 设置距离周期
     * @param distanceInterval
     */
    public static void setDistanceInterval(int distanceInterval) {
        Trace.distanceInterval = distanceInterval;
    }

    /***
     * 获取服务类型
     * @return
     */
    public static int getTraceType() {
        return traceType;
    }

    /**
     * 设置周期
     * @param gatherInterval 采集周期
     * @param packInterval   发包周期
     * @param distanceInterval 距离周期
     * @return
     */
    public static boolean setInterval(int gatherInterval, int packInterval, int distanceInterval) {
        if ((gatherInterval < 2) || (gatherInterval > 300) || (packInterval < gatherInterval) || (packInterval % gatherInterval != 0) || (packInterval > 300)) {
            return false;
        }
        Trace.gatherInterval = gatherInterval;
        Trace.packInterval = packInterval;
        Trace.distanceInterval = distanceInterval;
        return true;
    }


}
